package com.jcoinexchange.match.engine;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Queue;
import java.util.concurrent.ThreadFactory;

import static com.jcoinexchange.match.engine.Order.OrderSide.BUY;

// 每个交易对一个独立的 Disruptor 实例（分片设计）
public class MatchingEngine {
    private final Disruptor<OrderEvent> disruptor;
    private final RingBuffer<OrderEvent> ringBuffer;

    public MatchingEngine(String symbol) {
        disruptor = new Disruptor<OrderEvent>(
                new EventFactory<OrderEvent>() {
                    @Override
                    public OrderEvent newInstance() {
                        return null;
                    }
                },
                1024 * 1024,  // RingBuffer 大小
                new ThreadFactory() {
                    @Override
                    public Thread newThread(@NotNull Runnable r) {
                        return new Thread(r);
                    }
                },
                ProducerType.MULTI,  // 支持多生产者
                new BlockingWaitStrategy()
        );

        // 事件处理器：订单撮合逻辑
        disruptor.handleEventsWith(new MakeOrderEventHandler());
        ringBuffer = disruptor.start();
    }



}